package com.castle.fortress.admin.study.exam.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.exam.entity.StudyMemberUnfinishExamEntity;
import com.castle.fortress.admin.study.exam.dto.StudyMemberUnfinishExamDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.study.exam.service.StudyMemberUnfinishExamService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员未完成试卷信息 api 控制器
 *
 * @author castle
 * @since 2023-02-16
 */
@Api(tags="会员未完成试卷信息api管理控制器")
@RestController
@RequestMapping("/api/study/exam/studyMemberUnfinishExam")
public class ApiStudyMemberUnfinishExamController {
    @Autowired
    private StudyMemberUnfinishExamService studyMemberUnfinishExamService;


    /**
     * 会员未完成试卷信息的分页展示
     * @param studyMemberUnfinishExamDto 会员未完成试卷信息实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="会员未完成试卷信息-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员未完成试卷信息分页展示")
    @GetMapping("/page")
    public RespBody<IPage<StudyMemberUnfinishExamDto>> pageStudyMemberUnfinishExam(StudyMemberUnfinishExamDto studyMemberUnfinishExamDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyMemberUnfinishExamDto> page = new Page(pageIndex, pageSize);

        IPage<StudyMemberUnfinishExamDto> pages = studyMemberUnfinishExamService.pageStudyMemberUnfinishExam(page, studyMemberUnfinishExamDto);
        return RespBody.data(pages);
    }

    /**
     * 会员未完成试卷信息保存
     * @param studyMemberUnfinishExamDto 会员未完成试卷信息实体类
     * @return
     */
    @CastleLog(operLocation="会员未完成试卷信息-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("会员未完成试卷信息保存")
    @PostMapping("/save")
    public RespBody<String> saveStudyMemberUnfinishExam(@RequestBody StudyMemberUnfinishExamDto studyMemberUnfinishExamDto){
        if(studyMemberUnfinishExamDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyMemberUnfinishExamEntity studyMemberUnfinishExamEntity = ConvertUtil.transformObj(studyMemberUnfinishExamDto,StudyMemberUnfinishExamEntity.class);
        if(studyMemberUnfinishExamService.save(studyMemberUnfinishExamEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员未完成试卷信息编辑
     * @param studyMemberUnfinishExamDto 会员未完成试卷信息实体类
     * @return
     */
    @CastleLog(operLocation="会员未完成试卷信息-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("会员未完成试卷信息编辑")
    @PostMapping("/edit")
    public RespBody<String> updateStudyMemberUnfinishExam(@RequestBody StudyMemberUnfinishExamDto studyMemberUnfinishExamDto){
        if(studyMemberUnfinishExamDto == null || studyMemberUnfinishExamDto.getId() == null || studyMemberUnfinishExamDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyMemberUnfinishExamEntity studyMemberUnfinishExamEntity = ConvertUtil.transformObj(studyMemberUnfinishExamDto,StudyMemberUnfinishExamEntity.class);
        if(studyMemberUnfinishExamService.updateById(studyMemberUnfinishExamEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员未完成试卷信息删除
     * @param ids 会员未完成试卷信息id集合
     * @return
     */
    @CastleLog(operLocation="会员未完成试卷信息-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("会员未完成试卷信息删除")
    @PostMapping("/delete")
    public RespBody<String> deleteStudyMemberUnfinishExam(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyMemberUnfinishExamService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员未完成试卷信息详情
     * @param id 会员未完成试卷信息id
     * @return
     */
    @CastleLog(operLocation="会员未完成试卷信息-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员未完成试卷信息详情")
    @GetMapping("/info")
    public RespBody<StudyMemberUnfinishExamDto> infoStudyMemberUnfinishExam(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyMemberUnfinishExamEntity studyMemberUnfinishExamEntity = studyMemberUnfinishExamService.getById(id);
            StudyMemberUnfinishExamDto studyMemberUnfinishExamDto = ConvertUtil.transformObj(studyMemberUnfinishExamEntity,StudyMemberUnfinishExamDto.class);
        return RespBody.data(studyMemberUnfinishExamDto);
    }


}
